/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.sys.modular.sysappinfoimages.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.sys.modular.sysappinfofeatures.entity.SysAppInfoFeatures;
import vip.xiaonuo.sys.modular.sysappinfoimages.entity.SysAppInfoImages;
import vip.xiaonuo.sys.modular.sysappinfoimages.enums.SysAppInfoImagesExceptionEnum;
import vip.xiaonuo.sys.modular.sysappinfoimages.mapper.SysAppInfoImagesMapper;
import vip.xiaonuo.sys.modular.sysappinfoimages.param.SysAppInfoImagesParam;
import vip.xiaonuo.sys.modular.sysappinfoimages.service.SysAppInfoImagesService;

import javax.annotation.Resource;
import java.util.List;

/**
 * APP示例图service接口实现类
 *
 * @author yanwei
 * @date 2023-03-21 12:07:25
 */
@Service
public class SysAppInfoImagesServiceImpl extends ServiceImpl<SysAppInfoImagesMapper, SysAppInfoImages> implements SysAppInfoImagesService {

    @Override
    public PageResult<SysAppInfoImages> page(SysAppInfoImagesParam sysAppInfoImagesParam) {
        QueryWrapper<SysAppInfoImages> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(sysAppInfoImagesParam)) {

            // 根据图片地址 查询
            if (ObjectUtil.isNotEmpty(sysAppInfoImagesParam.getImageUrls())) {
                queryWrapper.lambda().eq(SysAppInfoImages::getImageUrls, sysAppInfoImagesParam.getImageUrls());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<SysAppInfoImages> list(SysAppInfoImagesParam sysAppInfoImagesParam) {
        return this.list();
    }

    @Override
    public void add(SysAppInfoImagesParam sysAppInfoImagesParam) {
        SysAppInfoImages sysAppInfoImages = new SysAppInfoImages();
        BeanUtil.copyProperties(sysAppInfoImagesParam, sysAppInfoImages);
        this.save(sysAppInfoImages);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SysAppInfoImagesParam> sysAppInfoImagesParamList) {
        sysAppInfoImagesParamList.forEach(sysAppInfoImagesParam -> {
            this.removeById(sysAppInfoImagesParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysAppInfoImagesParam sysAppInfoImagesParam) {
        SysAppInfoImages sysAppInfoImages = this.querySysAppInfoImages(sysAppInfoImagesParam);
        BeanUtil.copyProperties(sysAppInfoImagesParam, sysAppInfoImages);
        this.updateById(sysAppInfoImages);
    }

    @Override
    public SysAppInfoImages detail(SysAppInfoImagesParam sysAppInfoImagesParam) {
        return this.querySysAppInfoImages(sysAppInfoImagesParam);
    }

    /**
     * 获取APP示例图
     *
     * @author yanwei
     * @date 2023-03-21 12:07:25
     */
    private SysAppInfoImages querySysAppInfoImages(SysAppInfoImagesParam sysAppInfoImagesParam) {
        SysAppInfoImages sysAppInfoImages = this.getById(sysAppInfoImagesParam.getId());
        if (ObjectUtil.isNull(sysAppInfoImages)) {
            throw new ServiceException(SysAppInfoImagesExceptionEnum.NOT_EXIST);
        }
        return sysAppInfoImages;
    }

    @Override
    public void export(SysAppInfoImagesParam sysAppInfoImagesParam) {
        List<SysAppInfoImages> list = this.list(sysAppInfoImagesParam);
        PoiUtil.exportExcelWithStream("images.xls", SysAppInfoFeatures.class, list);
    }

}
